package com.realestate.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.ComUnitName;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbCommonAttribute;
import com.realestate.model.QBrtbCommonAttribute;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class CommonAttributeDaoImpl extends GenericDAOImpl<BrtbCommonAttribute, Long> implements CommonAttributeDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(CommonAttributeDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
    
 
    public List<BrtbCommonAttribute> getAllCommonAttributes() {
    	JPQLQuery query = new JPAQuery(em());
		QBrtbCommonAttribute commonAttribute = QBrtbCommonAttribute.brtbCommonAttribute;
		
		query.from(commonAttribute);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(commonAttribute.recordStatus.eq(RecordStatus.O));
		
		query.where(where);
		
		List<BrtbCommonAttribute> commonAttributes = query.list(commonAttribute);
		return commonAttributes;
    }


	@Override
	public BrtbCommonAttribute getCommonAttributeByCode(String code) {
		JPQLQuery query = new JPAQuery(em());
		QBrtbCommonAttribute commonAttribute = QBrtbCommonAttribute.brtbCommonAttribute;
		
		query.from(commonAttribute);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(commonAttribute.recordStatus.eq(RecordStatus.O)).and(commonAttribute.commonAttributeCode.eq(code));
		
		query.where(where);
		
		BrtbCommonAttribute commonAttributes = query.uniqueResult(commonAttribute);
		return commonAttributes;
	}
}
